package com.xyz.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.xyz.aclservice.entity.EduUser;
import com.xyz.aclservice.service.EduRoleService;
import com.xyz.aclservice.service.EduUserService;
import com.xyz.entity.R;
import com.xyz.utils.MD5;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Schema(description="用户管理接口")
@RestController
@RequestMapping("/admin/acl/user")
//@CrossOrigin
public class EduUserController {

    @Autowired
    private EduUserService eduUserService;

    @Autowired
    private EduRoleService eduRoleService;

    @Schema(description="获取管理用户分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @Schema(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,

            @Schema(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,

            @Schema(name = "courseQuery", description = "查询对象", required = false)
            EduUser eduUserQueryVo) {
        Page<EduUser> pageParam = new Page<>(page, limit);
        QueryWrapper<EduUser> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(eduUserQueryVo.getUsername())) {
            wrapper.like("username", eduUserQueryVo.getUsername());
        }

        IPage<EduUser> pageModel = eduUserService.page(pageParam, wrapper);
        return R.ok().data("items", pageModel.getRecords()).data("total", pageModel.getTotal());
    }

    @Schema(description="新增管理用户")
    @PostMapping("save")
    public R save(@RequestBody EduUser eduUser) {
        eduUser.setPassword(MD5.encrypt(eduUser.getPassword()));
        eduUserService.save(eduUser);
        return R.ok();
    }

    @Schema(description="修改管理用户")
    @PutMapping("update")
    public R updateById(@RequestBody EduUser eduUser) {
        eduUserService.updateById(eduUser);
        return R.ok();
    }

    @Schema(description="删除管理用户")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        eduUserService.removeById(id);
        return R.ok();
    }

    @Schema(description="根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<String> idList) {
        eduUserService.removeByIds(idList);
        return R.ok();
    }

    @Schema(description="根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = eduRoleService.findRoleByUserId(userId);
        return R.ok().data(roleMap);
    }



    @Schema(description="根据id获取用户信息")
    @GetMapping("/get/{id}")
    public R doAssign(@PathVariable("id") String userId) {
        EduUser eduUser = eduUserService.getById(userId);
        return R.ok().data("item", eduUser);
    }


}

